package gyb.rasa.controller.manage;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import gyb.rasa.common.utils.Result;
import gyb.rasa.entity.Hot;
import gyb.rasa.entity.Ip;
import gyb.rasa.entity.MessageReceived;
import gyb.rasa.entity.User;
import gyb.rasa.service.HotService;
import gyb.rasa.service.IpService;
import gyb.rasa.service.MessageReceivedService;
import gyb.rasa.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @Date 2023/5/16 23:33
 * @Author 郜宇博
 */
@RestController
@RequestMapping("/api/manage")
public class ManagerController {
    private final UserService userService;
    private final MessageReceivedService messageReceivedService;
    private final IpService ipService;
    private final HotService hotService;
    public ManagerController(UserService userService, MessageReceivedService messageReceivedService, IpService ipService, HotService hotService) {
        this.userService = userService;
        this.messageReceivedService = messageReceivedService;
        this.ipService = ipService;
        this.hotService = hotService;
    }
    @ApiOperation("修改用户信息")
    @PostMapping("user")
    public Result updateUser(@RequestBody User user){
        boolean updateUser = userService.updateById(user);
        return updateUser?Result.ok().message("修改用户数据成功"):Result.error().message("修改用户失败");
    }
    @ApiOperation("分页获取用户")
    @GetMapping("users/{current}/{limit}")
    public Result getUserByPage(@ApiParam(name = "current", value = "当前页码",required = true) @PathVariable Long current,
                                @ApiParam(name = "limit", value = "一页记录数",required = true) @PathVariable Long limit) {
        IPage<User> userList = userService.getUserByPage(current,limit);
        return Result.ok().data("userList",userList);

    }
    @ApiOperation("获取所有message")
    @GetMapping("messages/{current}/{limit}/{good}")
    public Result getMessageList(@ApiParam(name = "current", value = "当前页码",required = true) @PathVariable Long current,
                                 @ApiParam(name = "limit", value = "一页记录数",required = true) @PathVariable Long limit,
                                 @ApiParam(name = "good", value = "是否获取最佳回答",required = false) @PathVariable Integer good){
        IPage<MessageReceived> messageReceivedList = messageReceivedService.getMessageListByPage(current,limit,good);
        return Result.ok().data("messageList",messageReceivedList);
    }
    @ApiOperation("修改message信息")
    @PostMapping("message")
    public Result updateMessageReceived(@RequestBody MessageReceived messageReceived){
        if (StringUtils.isEmpty(messageReceived.getWhen()) ){
            String response = messageReceived.getResponse();
            ArrayList<String> emptyResponse = new ArrayList<>();
            emptyResponse.add(response);
            messageReceived.setResponse(emptyResponse.toString());

        }
        boolean updateMessage = messageReceivedService.updateById(messageReceived);
        return updateMessage?Result.ok().message("修改对话数据成功"):Result.error().message("修改对话数据失败");
    }
    @ApiOperation("分页获取IP")
    @GetMapping("ips/{current}/{limit}")
    public Result getIpByPage(@ApiParam(name = "current", value = "当前页码",required = true) @PathVariable Long current,
                                @ApiParam(name = "limit", value = "一页记录数",required = true) @PathVariable Long limit) {
        IPage<Ip> ipList = ipService.getIpByPage(current,limit);
        return Result.ok().data("ipList",ipList);

    }
    @ApiOperation("分页获取Hot")
    @GetMapping("hots/{current}/{limit}")
    public Result getHotByPage(@ApiParam(name = "current", value = "当前页码",required = true) @PathVariable Long current,
                              @ApiParam(name = "limit", value = "一页记录数",required = true) @PathVariable Long limit) {
        IPage<Hot> hotList = hotService.getHotByPage(current,limit);
        return Result.ok().data("hotList",hotList);

    }
    @ApiOperation("修改ip信息")
    @PostMapping("ip")
    public Result updateIp(@RequestBody Ip ip){
        boolean updateIp = ipService.updateById(ip);
        return updateIp?Result.ok().message("修改Ip数据成功"):Result.error().message("修改Ip数据失败");
    }
    @ApiOperation("修改Hot信息")
    @PostMapping("hot")
    public Result updateHot(@RequestBody Hot hot){
        boolean updateHot = hotService.updateById(hot);
        return updateHot?Result.ok().message("修改Hot数据成功"):Result.error().message("修改Hot数据失败");
    }
}
